<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\blog\model;
use think\Model;
use think\db;

class CouponModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
        $id=cmf_get_current_admin_id();
        $this->id=$id;
    }
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $cause['id']=$id;
        $user=DB::name('user')->where($cause)->find();
        if($user['user_type']==1)
        {
            $where='';
            $session=DB::name('shop')->where($where)->select();
        }else{
            $where['re_guid']=$user['shop_guid'];
            $session=DB::name('shop')->where($where)->select()->toArray();
        }
        return $session;
    }
   /*优惠券列表*/
   public function index($cause)
   {
       $session=$this->session();
       if($session)
       {
           if($this->id==1)
           {
               $where="";
           }else{
               $where['co.shop_id']=$session[0]['re_guid'];
           }
           if(isset($cause['nick_name']))
           {
               if($cause['nick_name'])
               {
                   $where['me.nick_name']=array('like','%'.$cause['nick_name'].'%');
               }
           }
           if(isset($cause['id']))
           {
               if($cause['id'])
               {
                   $where['me.id']=$cause['id'];
               }
           }
           $data=DB::name('coupon')->alias('co')->join('member me','co.voucher_memberid = me.m_guid')->where($where)->field('co.*,me.nick_name')->paginate(10);
           if($data->toArray())
           {
               foreach ($data as $k=>$v)
               {
                   $data->toArray();
                   $date=array();
                   $date=$v;
                   if($v['voucher_use']==1){
                       $date['voucher_name']='使用';
                   }else{
                       $date['voucher_name']='未使用';
                   }
                   $date['voucher']='满'.$v['voucher_cause'].'减'.$v['voucher_money'];
                   $data->offsetSet($k,$date);
               }
           }
           $data->appends($cause);
           $array['data']=$data;
           $array['page']=$data->render();
       }else{
           $array['data']="";
           $array['page']="";
       }
       return $array;

   }
   /*
    * 添加优惠券
    * */
   public function add($data)
   {
      if(isset($data['shop']))
      {
          $date='';
          if(isset($data['userid']))
          {
              $userid=explode(',',$data['userid']);
              $where['cou_guid']=$data['coupon'];
              $cou=db::name('cou')->where($where)->find();
              foreach ($userid as $k=>$v)
              {
                  if($v)
                  {
                      $array['cou_guid']=guid();
                      $array['voucher_memberid']=$v;
                      $array['voucher_cause']=$cou['cause'];
                      $array['voucher_money']=$cou['money'];
                      $array['u_time']=$cou['s_time'];
                      $array['e_time']=$cou['e_time'];
                      $array['voucher_cause']=$cou['cause'];
                      $array['shop_id']=$data['shop'];
                      $date[$k]=$array;
                  }
              }

          }
          $status=Db::name('coupon')->insertAll($date);
      }else{
          $status=0;
      }
       return $status;
   }
   /**
    * 根据id获取列表记录
   */
   public function getcone($where){
       $status= DB::name('cou')->where($where)->select();
       return $status;
   }
   /*获取会员*/
   public function user($array,$status)
   {

       $session=$this->session();
       if($session)
       {
           if($this->id==1)
           {
               $where="";
           }else{
               $where['shop_id']=$session[0]['re_guid'];
           }
           if(isset($array['nick_name']))
           {
               if($array['nick_name'])
               {
                   $where['nick_name']=array('like','%'.$array['nick_name'].'%');
               }
           }
           if(isset($array['id']))
           {
               if($array['id'])
               {
                   $where['id']=$array['id'];
               }
           }
           if($status==1)
           {
               $user=DB::name('member')->where($where)->paginate(10);
               $user->appends($where);
               $data['data']=$user;
               $data['page']=$user->render();
           }else{
               $data=DB::name('member')->where($where)->find();
           }
       }else{
           $data['data']="";
           $data['page']="";
       }
       return $data;
   }

}